﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.UpdateContinuousMfg.CCUiUCMUpdateContinuousMfgDlg
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Resources;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands.UpdateContinuousMfg;

public class CCUiUCMUpdateContinuousMfgDlg : TxForm
{
  private const string _PRESERVED_ATTRIBUTE = "Preserved";
  private const string _STRING_TABLE_LOCATION = "DnProcessSimulateCommands.UpdateContinuousMFG.UCMStringTable";
  private const int _EMPHASIZED_SEAMS_WIDTH = 3;
  private ResourceManager _resourceManager;
  private TxSeamMfgFeature _selectedSeam;
  private IContainer components;
  private Label lblSourceCurve;
  private TxObjEditBoxCtrl txebSourceCurve;
  private Label lblAssignMFGToPart;
  private TxObjEditBoxCtrl txebAssigmMFGToPart;
  private Button btnCancel;
  private Button btnCreate;

  public CCUiUCMUpdateContinuousMfgDlg()
  {
    this.InitializeComponent();
    this._resourceManager = new ResourceManager("DnProcessSimulateCommands.UpdateContinuousMFG.UCMStringTable", ((object) this).GetType().Assembly);
  }

  public virtual void OnInitTxForm()
  {
    // ISSUE: unable to decompile the method.
  }

  private void CCUiCMCCreateContinuousMFGFromCurveDlg_Load(object sender, EventArgs e)
  {
    this.GetSelectedMfg();
    ((Control) this).Text = string.Format(this._resourceManager.GetString("UCM_DialogTitle"), (object) ((TxMfgFeature) this._selectedSeam).Name);
    this.txebAssigmMFGToPart.Object = this.GetPartFromSelectedMfg();
    if (this.txebAssigmMFGToPart.Object == null)
      this.txebAssigmMFGToPart.Focus();
    else
      this.txebSourceCurve.Focus();
    this.EnableOrDisableCreateButton();
  }

  private void GetSelectedMfg()
  {
    this._selectedSeam = ((Collection<ITxObject>) TxApplication.ActiveSelection.GetAllItems())[0] as TxSeamMfgFeature;
  }

  private void btnCancel_Click(object sender, EventArgs e) => ((Form) this).Close();

  private void btnCreate_Click(object sender, EventArgs e)
  {
    Cursor current = Cursor.Current;
    Cursor.Current = Cursors.WaitCursor;
    bool flag = this.ChangeCurve();
    if (flag)
      this.ChangeAssignAndAttachToPart(this.txebAssigmMFGToPart.Object);
    this._selectedSeam.StartPoint = ((TxMfgFeature) this._selectedSeam).AbsoluteLocation.Inverse;
    this._selectedSeam.EndPoint = ((TxMfgFeature) this._selectedSeam).AbsoluteLocation.Inverse;
    this._selectedSeam.IntermediatePoint = ((TxMfgFeature) this._selectedSeam).AbsoluteLocation.Inverse;
    if (!flag)
    {
      int num = (int) TxMessageBox.Show(this._resourceManager.GetString("UMC_ChangeSourceCurveMessage"), this._resourceManager.GetString("UMC_ErrorTitle"), MessageBoxButtons.OK, MessageBoxIcon.Hand);
      this.txebSourceCurve.Focus();
    }
    Cursor.Current = current;
    if (!flag)
      return;
    ((Form) this).Close();
  }

  private void txebSourceCurve_Picked(object sender, TxObjEditBoxCtrl_PickedEventArgs args)
  {
    this.EnableOrDisableCreateButton();
  }

  private void txebSourceCurve_TypeValid(object sender, EventArgs e)
  {
    this.EnableOrDisableCreateButton();
  }

  private void txebSourceCurve_TypeInvalid(object sender, EventArgs e)
  {
    this.EnableOrDisableCreateButton();
  }

  private bool ChangeCurve()
  {
    TxDisplayableObjectVisibility visibility = (this.txebSourceCurve.Object as ITxDisplayableObject).Visibility;
    (this.txebSourceCurve.Object as ITxDisplayableObject).Display();
    ITxObject itxObject = this.txebSourceCurve.Object;
    itxObject.RemoveAttribute("Preserved");
    bool flag = TxApplication.ActiveDocument.MfgRoot.UpdateSeamMfgFeature(this._selectedSeam, itxObject);
    if (visibility == 2)
      (this.txebSourceCurve.Object as ITxDisplayableObject).Blank();
    this.EmphasizeSeamIfWeAreInEmphasizeMode();
    return flag;
  }

  private void EmphasizeSeamIfWeAreInEmphasizeMode()
  {
    if (TxApplication.CommandsManager.IsCommandChecked("CApECMEmphasizeContinuousMfgCmd") && TxSeamMfgFeature.op_Inequality(this._selectedSeam, (ITxObject) null))
      this._selectedSeam.SetLineWidth(3);
    TxApplication.RefreshDisplay();
  }

  private void txebAssigmMFGToPart_Picked(object sender, TxObjEditBoxCtrl_PickedEventArgs args)
  {
    this.EnableOrDisableCreateButton();
  }

  private void txebAssigmMFGToPart_TypeInvalid(object sender, EventArgs e)
  {
    this.EnableOrDisableCreateButton();
  }

  private void txebAssigmMFGToPart_TypeValid(object sender, EventArgs e)
  {
    this.EnableOrDisableCreateButton();
  }

  private ITxObject GetPartFromSelectedMfg()
  {
    return TxSeamMfgFeature.op_Inequality(this._selectedSeam, (ITxObject) null) && ((Collection<ITxObject>) ((TxMfgFeature) this._selectedSeam).AssignedParts).Count > 0 ? ((Collection<ITxObject>) ((TxMfgFeature) this._selectedSeam).AssignedParts)[0] : (ITxObject) null;
  }

  private void ChangeAssignAndAttachToPart(ITxObject newPart)
  {
    if (!TxSeamMfgFeature.op_Inequality(this._selectedSeam, (ITxObject) null))
      return;
    if (newPart == null)
    {
      this.DisconnectMFGFromOldPart();
    }
    else
    {
      bool flag = true;
      TxObjectList assignedParts = ((TxMfgFeature) this._selectedSeam).AssignedParts;
      if (((Collection<ITxObject>) assignedParts).Count > 0)
      {
        if (((Collection<ITxObject>) assignedParts)[0] == newPart)
          flag = false;
        else
          ((Collection<ITxObject>) assignedParts).RemoveAt(0);
      }
      if (flag)
      {
        TxObjectList txObjectList = new TxObjectList();
        if (newPart != null)
          ((Collection<ITxObject>) txObjectList).Add(newPart);
        foreach (ITxObject itxObject in (Collection<ITxObject>) assignedParts)
          ((Collection<ITxObject>) txObjectList).Add(itxObject);
        ((TxMfgFeature) this._selectedSeam).ClearAssignedParts();
        ((TxMfgFeature) this._selectedSeam).AssignParts(txObjectList);
        this._selectedSeam.LeadingPart = newPart as ITxComponent;
      }
      if (((TxMfgFeature) this._selectedSeam).AttachmentParent == newPart)
        return;
      ITxLocatableObject itxLocatableObject = newPart as ITxLocatableObject;
      ITxLocatableObject selectedSeam = (ITxLocatableObject) this._selectedSeam;
      if (itxLocatableObject == null || selectedSeam == null || !selectedSeam.CanAttachTo(itxLocatableObject))
        return;
      selectedSeam.Detach();
      selectedSeam.AttachTo(itxLocatableObject);
    }
  }

  private void DisconnectMFGFromOldPart()
  {
    TxObjectList assignedParts = ((TxMfgFeature) this._selectedSeam).AssignedParts;
    if (((Collection<ITxObject>) assignedParts).Count > 0)
    {
      ((Collection<ITxObject>) assignedParts).RemoveAt(0);
      if (((Collection<ITxObject>) assignedParts).Count > 0)
      {
        ((TxMfgFeature) this._selectedSeam).ClearAssignedParts();
        ((TxMfgFeature) this._selectedSeam).AssignParts(assignedParts);
      }
      else
        ((TxMfgFeature) this._selectedSeam).ClearAssignedParts();
    }
    if (((TxMfgFeature) this._selectedSeam).AttachmentParent == null)
      return;
    ITxLocatableObject selectedSeam = (ITxLocatableObject) this._selectedSeam;
    selectedSeam.Detach();
    if (((Collection<ITxObject>) assignedParts).Count <= 0 || !(((Collection<ITxObject>) assignedParts)[0] is ITxLocatableObject itxLocatableObject) || selectedSeam == null || !selectedSeam.CanAttachTo(itxLocatableObject))
      return;
    selectedSeam.AttachTo(itxLocatableObject);
  }

  private void EnableOrDisableCreateButton()
  {
    bool flag = false;
    if (this.txebSourceCurve.Object != null && (this.txebAssigmMFGToPart.Object != null || !(((Control) this.txebAssigmMFGToPart).Text != string.Empty)))
      flag = true;
    this.btnCreate.Enabled = flag;
  }

  protected virtual void Dispose(bool disposing)
  {
    if (disposing && this.components != null)
      this.components.Dispose();
    base.Dispose(disposing);
  }

  private void InitializeComponent()
  {
    ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof (CCUiUCMUpdateContinuousMfgDlg));
    this.lblSourceCurve = new Label();
    this.txebSourceCurve = new TxObjEditBoxCtrl();
    this.lblAssignMFGToPart = new Label();
    this.txebAssigmMFGToPart = new TxObjEditBoxCtrl();
    this.btnCancel = new Button();
    this.btnCreate = new Button();
    ((Control) this).SuspendLayout();
    componentResourceManager.ApplyResources((object) this.lblSourceCurve, "lblSourceCurve");
    this.lblSourceCurve.Name = "lblSourceCurve";
    this.txebSourceCurve.KeepFaceEmphasizedWhenControlIsNotFocused = true;
    this.txebSourceCurve.ListenToPick = false;
    componentResourceManager.ApplyResources((object) this.txebSourceCurve, "txebSourceCurve");
    ((Control) this.txebSourceCurve).Name = "txebSourceCurve";
    this.txebSourceCurve.Object = (ITxObject) null;
    this.txebSourceCurve.PickAndClear = false;
    this.txebSourceCurve.PickLevel = (TxPickLevel) 1;
    this.txebSourceCurve.PickOnly = false;
    this.txebSourceCurve.ReadOnly = false;
    this.txebSourceCurve.ValidatorType = (TxValidatorType) 1;
    this.txebSourceCurve.TypeInvalid += new EventHandler(this.txebSourceCurve_TypeInvalid);
    this.txebSourceCurve.TypeValid += new EventHandler(this.txebSourceCurve_TypeValid);
    this.txebSourceCurve.Picked += new TxObjEditBoxCtrl_PickedEventHandler(this.txebSourceCurve_Picked);
    componentResourceManager.ApplyResources((object) this.lblAssignMFGToPart, "lblAssignMFGToPart");
    this.lblAssignMFGToPart.Name = "lblAssignMFGToPart";
    this.txebAssigmMFGToPart.KeepFaceEmphasizedWhenControlIsNotFocused = true;
    this.txebAssigmMFGToPart.ListenToPick = false;
    componentResourceManager.ApplyResources((object) this.txebAssigmMFGToPart, "txebAssigmMFGToPart");
    ((Control) this.txebAssigmMFGToPart).Name = "txebAssigmMFGToPart";
    this.txebAssigmMFGToPart.Object = (ITxObject) null;
    this.txebAssigmMFGToPart.PickAndClear = false;
    this.txebAssigmMFGToPart.PickLevel = (TxPickLevel) 1;
    this.txebAssigmMFGToPart.PickOnly = false;
    this.txebAssigmMFGToPart.ReadOnly = false;
    this.txebAssigmMFGToPart.ValidatorType = (TxValidatorType) 3;
    this.txebAssigmMFGToPart.TypeInvalid += new EventHandler(this.txebAssigmMFGToPart_TypeInvalid);
    this.txebAssigmMFGToPart.TypeValid += new EventHandler(this.txebAssigmMFGToPart_TypeValid);
    this.txebAssigmMFGToPart.Picked += new TxObjEditBoxCtrl_PickedEventHandler(this.txebAssigmMFGToPart_Picked);
    componentResourceManager.ApplyResources((object) this.btnCancel, "btnCancel");
    this.btnCancel.Name = "btnCancel";
    this.btnCancel.UseVisualStyleBackColor = true;
    this.btnCancel.Click += new EventHandler(this.btnCancel_Click);
    componentResourceManager.ApplyResources((object) this.btnCreate, "btnCreate");
    this.btnCreate.Name = "btnCreate";
    this.btnCreate.UseVisualStyleBackColor = true;
    this.btnCreate.Click += new EventHandler(this.btnCreate_Click);
    componentResourceManager.ApplyResources((object) this, "$this");
    ((ContainerControl) this).AutoScaleMode = AutoScaleMode.Font;
    ((Control) this).Controls.Add((Control) this.btnCreate);
    ((Control) this).Controls.Add((Control) this.btnCancel);
    ((Control) this).Controls.Add((Control) this.txebAssigmMFGToPart);
    ((Control) this).Controls.Add((Control) this.lblAssignMFGToPart);
    ((Control) this).Controls.Add((Control) this.txebSourceCurve);
    ((Control) this).Controls.Add((Control) this.lblSourceCurve);
    ((Form) this).FormBorderStyle = FormBorderStyle.FixedDialog;
    ((Form) this).MaximizeBox = false;
    ((Form) this).MinimizeBox = false;
    ((Control) this).Name = nameof (CCUiUCMUpdateContinuousMfgDlg);
    ((Form) this).Load += new EventHandler(this.CCUiCMCCreateContinuousMFGFromCurveDlg_Load);
    ((Control) this).ResumeLayout(false);
    ((Control) this).PerformLayout();
  }
}
